package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.*;
import com.itheima.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        log.info("aaa:"+orders.toString());
        ordersService.submit(orders,session);
        return  R.success("sa");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page,Integer pageSize,HttpSession session){
        log.info("page:{},pageSize:{},name:{}",page,pageSize);
        Page<Orders> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();
        qw.orderByDesc(Orders::getOrderTime);
        qw.eq(Orders::getUserId,session.getAttribute("user"));
        Page<Orders> page2 = ordersService.page(page1,qw);
        if (page2==null){
            return R.error("查询失败！");
        }
        return R.success(page2);
    }

    @GetMapping("/page")
    public R<Page<Orders>> Page(Integer page,Integer pageSize,HttpSession session){
        log.info("page:{},pageSize:{},name:{}",page,pageSize);
        Page<Orders> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();
        qw.orderByDesc(Orders::getOrderTime);
        Page<Orders> page2 = ordersService.page(page1,qw);
        if (page2==null){
            return R.error("查询失败！");
        }
        return R.success(page2);
    }

    //修改订单状态
    @PutMapping
    public R<String> setOrderStatus(@RequestBody Orders orders){

        log.info(orders.toString());
        Orders byId = ordersService.getById(orders.getId());
        byId.setStatus(orders.getStatus());
        ordersService.updateById(byId);
        return R.success("派送成功！");
    }

}
